﻿using System;
using System.Web.UI.WebControls;
using BLL;
using COMMON;
using VO;

namespace PopPromotoraCallCenter.Admin
{
    public partial class StatusCadastro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CarregarGridStatus();
            }
        }

        #region Metodos

        #region CarregarGridStatus
        protected void CarregarGridStatus()
        {
            var retorno = StatusAtendimentoBLL.Select();
            if (retorno.Error == Enumerator.Error.Success)
            {
                gvStatus.DataSource = retorno.List;
                gvStatus.DataBind();
            }
            else
                Funcao.Alert(ClientScript, retorno.Message);
        }
        #endregion

        #region LimparCampos
        protected void LimparCampos()
        {
            txtNome.Text = string.Empty;
            txtDescricao.Text = string.Empty;
            hfStatusId.Value = "0";
            cbAtivo.Checked = true;
        }
        #endregion

        #region GravarStatus
        protected void GravarStatus()
        {
            if (ValidarStatus())
            {
                var obj = new StatusAtendimento();
                obj.idStatus = int.Parse(hfStatusId.Value);
                obj.nome = txtNome.Text.Trim();
                obj.descricao = txtDescricao.Text.Trim();
                obj.fgAtivo = cbAtivo.Checked;
                var retorno = new Retorno<StatusAtendimento>();
                if (obj.idStatus == 0)
                    retorno = StatusAtendimentoBLL.Insert(obj);
                else
                    retorno = StatusAtendimentoBLL.Update(obj);
                if (retorno.Error == Enumerator.Error.Success)
                {
                    Funcao.Alert(ClientScript, "Gravado com sucesso");
                    CarregarGridStatus();
                    mvStatus.ActiveViewIndex = 0;
                }
                else
                    Funcao.Alert(ClientScript, retorno.Message);
            }
        }
        #endregion

        #region Editar
        protected void Editar(int idStatus)
        {
            var retorno = StatusAtendimentoBLL.Select(idStatus);
            if (retorno.Error == Enumerator.Error.Success)
            {
                mvStatus.ActiveViewIndex = 1;
                var list = retorno.List;
                hfStatusId.Value = list[0].idStatus.ToString();
                txtNome.Text = list[0].nome;
                txtDescricao.Text = list[0].descricao;
                cbAtivo.Checked = list[0].fgAtivo;
            }
            else
                Funcao.Alert(ClientScript, retorno.Message);
        }
        #endregion

        #region Excluir
        protected void Excluir(int idStatus)
        {
            var retorno = StatusAtendimentoBLL.Delete(idStatus);
            if (retorno.Error == Enumerator.Error.Success)
            {
                Funcao.Alert(ClientScript, "Excluído com sucesso");
                CarregarGridStatus();
            }
            else
                Funcao.Alert(ClientScript, retorno.Message);
        }
        #endregion

        #region ValidarStatus
        protected bool ValidarStatus()
        {
            string msg = string.Empty;
            if (string.IsNullOrWhiteSpace(txtNome.Text))
                msg = "Preencha o campo Nome status";
            else if (string.IsNullOrWhiteSpace(txtDescricao.Text))
                msg = "Preencha o campo Descrição status";

            bool retorno = string.IsNullOrWhiteSpace(msg);
            if (!retorno)
                Funcao.Alert(ClientScript, msg);

            return retorno;
        }
        #endregion

        #region HabilitarDesabilitar
        protected void HabilitarDesabilitar(int idStatus, bool statusAtual)
        {
            var obj = new StatusAtendimento();
            obj.idStatus = idStatus;
            obj.fgAtivo = !statusAtual;
            var retorno = StatusAtendimentoBLL.UpdateStatus(obj);
            if (retorno.Error == Enumerator.Error.Success)
                Funcao.Alert(ClientScript, "Status atualizado");
            else
                Funcao.Alert(ClientScript, retorno.Message);
            CarregarGridStatus();
        }
        #endregion

        #endregion

        #region Eventos

        #region btnNovo_Click
        protected void btnNovo_Click(object sender, EventArgs e)
        {
            LimparCampos();
            mvStatus.ActiveViewIndex = 1;
        }
        #endregion

        #region gvStatus_RowCommand
        protected void gvStatus_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int idStatus = 0;
            switch (e.CommandName)
            {
                case "editar":
                    idStatus = int.Parse(e.CommandArgument.ToString());
                    Editar(idStatus);
                    break;
                case "excluir":
                    idStatus = int.Parse(e.CommandArgument.ToString());
                    Excluir(idStatus);
                    break;
                case "status":
                    int index = int.Parse(e.CommandArgument.ToString());
                    idStatus = int.Parse(gvStatus.DataKeys[index]["idStatus"].ToString());
                    bool status = bool.Parse(gvStatus.DataKeys[index]["fgAtivo"].ToString());
                    HabilitarDesabilitar(idStatus, status);
                    break;
            }
        }
        #endregion

        #region gvStatus_RowDataBound
        protected void gvStatus_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                bool status = bool.Parse(gvStatus.DataKeys[e.Row.RowIndex]["fgAtivo"].ToString());
                var botao = e.Row.FindControl("btnStatus") as Button;
                if (botao != null)
                {
                    if (status)
                        botao.Text = "Desabilitar";
                    else
                        botao.Text = "Habilitar";
                }
            }
        }
        #endregion

        #region btnGravar_Click
        protected void btnGravar_Click(object sender, EventArgs e)
        {
            GravarStatus();
        }
        #endregion

        #region btnVoltar_Click
        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            mvStatus.ActiveViewIndex = 0;
        }
        #endregion

        #endregion

    }
}